package com.neusoft.databus.common.connection;

import com.neusoft.bizcore.web.utils.JsonUtils;

import lombok.Getter;
import lombok.Setter;

/**
 * serial连接配置
 *
 * @author naxiang
 */
@Getter
@Setter
public class SerialConnection extends AbstractConnection {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    // 串口
    public String comValue;
    // 奇偶校验
    public int parity;
    // 数据位
    public int dataBits;
    // 停止位
    public int stopBits;
    // 串行口上的波特率
    public int baudrate;
    // sotimeout
    public int sotimeout = 1000;
    // 重试次数
    public int retry = 0;

    @Override
    public int hashCode() {
        return this.comValue.hashCode();
    }

    @Override
    public boolean equals(final Object o) {
        if (o instanceof SerialConnection) {
            return this.hashCode() == ((SerialConnection) o).hashCode();
        }
        return false;
    }

    @Override
    public SerialConnection asUsedConnection() {
        final SerialConnection connection = new SerialConnection();
        connection.setComValue(this.comValue);
        connection.setParity(this.parity);
        connection.setDataBits(this.dataBits);
        connection.setStopBits(this.stopBits);
        connection.setBaudrate(this.baudrate);
        connection.setRetry(this.retry);
        connection.setSotimeout(this.sotimeout);
        return connection;
    }

    @Override
    public String toString() {
        return JsonUtils.pojoToJson(this);
    }
}
